package org.example.reader;

import org.apache.parquet.example.data.Group;
import org.apache.parquet.hadoop.ParquetReader;
import org.apache.parquet.hadoop.example.GroupReadSupport;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
import org.apache.parquet.hadoop.metadata.ParquetMetadata;
import org.apache.parquet.hadoop.ParquetFileReader;
import org.apache.parquet.schema.MessageType;

import java.io.IOException;

public class DeltaParquetReader {

    // 最大打印行数
    private static final int MAX_ROWS_TO_PRINT = 5;

    // 打印文件结构信息
    private void printSchema(String filePath) throws IOException {
        try (ParquetFileReader reader = ParquetFileReader.open(
                new Configuration(),
                new Path(filePath))) {
            ParquetMetadata metadata = reader.getFooter();
            MessageType schema = metadata.getFileMetaData().getSchema();
            
            System.out.println("Parquet文件结构：");
            System.out.println(schema);
            System.out.println("行组数量：" + metadata.getBlocks().size());
            System.out.println("总行数：" + metadata.getFileMetaData().getKeyValueMetaData().get("numRows"));
            System.out.println();
        }
    }

    public void readParquetFile(String filePath) throws IOException {
        // 先打印文件结构
        printSchema(filePath);

        // 创建ParquetReader
        ParquetReader<Group> reader = ParquetReader.builder(new GroupReadSupport(), new Path(filePath))
                .withConf(new Configuration())
                .build();

        try {
            Group group;
            int rowCount = 0;
            // 逐行读取文件内容
            while ((group = reader.read()) != null) {
                if (rowCount < MAX_ROWS_TO_PRINT) {
                    // 打印前5行内容
                    System.out.println(group.toString());
                }
                rowCount++;
            }
            
            // 如果总行数超过5行，打印省略号
            if (rowCount > MAX_ROWS_TO_PRINT) {
                System.out.println("...");
                System.out.println("总行数：" + rowCount);
            }
        } finally {
            // 确保关闭reader
            reader.close();
        }
    }

    public static void main(String[] args) {
        // 将文件路径变量移动到main方法中
        final String PARQUET_FILE_PATH = "/tmp/paimon/default.db/t_user/index/index-7a37cae1-05a2-420f-ad60-7c86db6ef542-0";
        
        DeltaParquetReader reader = new DeltaParquetReader();
        
        try {
            System.out.println("开始读取Parquet文件：" + PARQUET_FILE_PATH);
            reader.readParquetFile(PARQUET_FILE_PATH);
            System.out.println("文件读取完成");
        } catch (IOException e) {
            System.err.println("读取文件时发生错误：" + e.getMessage());
        }
    }
}
